package board.action;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import board.dao.MemberDao;
import board.vo.Member;

import com.opensymphony.xwork2.ActionSupport;

public class MemberAction extends ActionSupport implements SessionAware {
	
	private Map<String, Object> session;
	
	private String id;
	private String password;
	private String name;
	private String rights_value;
	private Member member;
	private boolean loginFail;
	
	
	
	public String login() {
		
		System.out.println("MemberAction.login()> id: " + id + " password: " + password);
		
		member = new MemberDao().selectOneMember(id, password);
		
		if(member == null) {
			loginFail = true;
			
			System.out.println("MemberAction.login()> loginFail:" + loginFail);
			
		} else {
			session.put("id", member.getId()); session.put("name", member.getName());
			
			System.out.println("MemberAction.login()> member:" + member);
					
		}
		
		return SUCCESS;
	}//login()
	
	public String goMenu() {
		
		System.out.println("MemberAction.goMenu()> launch!");
		member = new MemberDao().selectOneMember((String) session.get("id"));
		
		return SUCCESS;
	}//goMenu
	
	public String changeLogin_status(){
		
		member = new MemberDao().selectOneMember((String) session.get("id"));
		
		if(member.getLogin_status() == 0){
			System.out.println("MemberAction.updateOneMember(member)> login_status: 1");
			member.setLogin_status(1);
			new MemberDao().updateOneMember(member);
		}
		
		return SUCCESS;
	}
	
	
	public String setPassword(){
		
		System.out.println("MemberAction.setPassword()> password: " + password);
		member = new MemberDao().selectOneMember((String) session.get("id"));
		member.setPassword(password);
		
		new MemberDao().updateOneMember(member);
		return SUCCESS;
	}
	
	public String insertMember(){
		System.out.println("MemberAction.selectRights()> rights_value: " + rights_value);
		new MemberDao().insertMember(id,name,password,new MemberDao().selectRights(rights_value));
		
		return SUCCESS;
	}
	
	public String goSearchMember(){
	
		return SUCCESS;
	}
	
	public String goUpdateMember(){
		
		System.out.println("MemberAction.selectOneMember()> id: " + id);
		member = new MemberDao().selectOneMember(id);
		
		return SUCCESS;
	}
	
	public String updateMember(){
		
		System.out.println("MemberAction.updateMember()> id: " + id);
		
		member = new MemberDao().selectOneMember(id);
		member.setName(name);
		member.setPassword(password);
		member.setRights(new MemberDao().selectRights(rights_value));
		
		//System.out.println("MemberAction.updateMember()> member: " + member);
		new MemberDao().updateOneMember(member);
		
		return SUCCESS;
	}
	
	public String deleteMember(){
		
		System.out.println("MemberAction.deleteMember()> id: "+ id);
		
		new MemberDao().deleteMember(id);
		
		return SUCCESS;
	}
	
	
	
	
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
		
	}//setSession()

	
	/*
	 * setters and getters
	 */
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}
	
	public boolean isLoginFail() {
		return loginFail;
	}

	public void setLoginFail(boolean loginFail) {
		this.loginFail = loginFail;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getRights_value() {
		return rights_value;
	}

	public void setRights_value(String rights_value) {
		this.rights_value = rights_value;
	}
	
	
	
	

}//class
